package com.example.educationblockchain.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.educationblockchain.mapper.AdminLoginMapper;
import com.example.educationblockchain.model.AdminLogin;
import com.example.educationblockchain.websocket.P2PClient;
import com.example.educationblockchain.websocket.P2PServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class Runner implements ApplicationRunner {

    @Autowired
    BlockCache blockCache;

    @Autowired
    P2PServer p2PServer;

    @Autowired
    P2PClient p2PClient;

    @Autowired
    AdminLoginMapper adminLoginMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("开始");

        int adminNum = adminLoginMapper.selectCount(new QueryWrapper<AdminLogin>());
        if(adminNum==0){
            AdminLogin adminLogin= new AdminLogin("admin","password");
            adminLoginMapper.insertAdminLogin(adminLogin);
        }
        p2PServer.initP2PServer(blockCache.getP2pport());
        //p2PClient.connectToPeer(blockCache.getAddress());
        blockCache.setPassword(Encrypted.getRandomString(16));
        System.out.println("*****端口号******"+blockCache.getP2pport());
        System.out.println("本机IP:"+CommonUtil.getLocalIp());
    }
}
